<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>更新站点</title>
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css"/>
    <style>
        .layui-form-checkbox > div {
            padding: 0 20px 0 0;
            font-size: 14px;
            border-radius: 2px 0 0 2px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
    </style>
</head>
<body>

<form class="layui-form" lay-filter="update-filter">

    <div class="mainBox">
        <div class="main-container mr-5">

            <div class="layui-form-item">
                <label class="layui-form-label required">站点名称</label>
                <div class="layui-input-block">
                    <input type="text" name="site" value="" required lay-verify="required" class="layui-input" disabled>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">禁用</label>
                <div class="layui-input-block">
                    <input type="checkbox" id="disabled" lay-filter="disabled" lay-skin="switch"/>
                    <input type="text" style="display:none" name="disabled" value="0"/>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">镜像域名</label>
                <div class="layui-input-block">
                    <input type="text" name="mirror" value="" placeholder="请输入镜像域名，例：pt.baidu.com"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" id="cookie_required_label">Cookie</label>
                <div class="layui-input-block">
                    <input type="text" name="cookie" value="" class="layui-input" id="cookie_required_input">
                </div>
            </div>

            <?=$html?>

        </div>
    </div>

    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-md">
                重置
            </button>
        </div>
    </div>

</form>

<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>

<script>
    console.log(location.href, location);
    /**
     * 帮助
     * @type {{qq_qun: string}}
     */
    window.IYUU_HELP = JSON.parse(localStorage.getItem('IYUU_HELP'));
    // 相关接口
    const PRIMARY_KEY = "id";
    const FIND_API = "/admin/site/find" + location.search;
    const UPDATE_API = "/admin/site/update";

    // 获取数据库记录
    layui.use(["form", "util", "popup"], function () {
        let $ = layui.$;
        let util = layui.util;
        let popup = layui.popup;
        let form = layui.form;

        /**
         * 初始化元素的属性值
         * @param {string} selector
         * @param {string|int} value
         */
        function init_element_attr_value(selector, value) {
            let obj = $(selector);
            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;

            console.log('初始化' + obj[0].nodeName.toLowerCase() + '属性值', selector, value)
            if (obj[0].nodeName.toLowerCase() === "textarea") {
                obj.val(value);
            } else {
                obj.attr("value", value);
                obj[0].value = value;
                if ('checkbox' === obj.attr('type')) {
                    obj.attr('checked', true);
                } else if (obj[0].nodeName.toLowerCase() === "select") {
                    obj.find('option[value="' + value + '"]').attr('selected', true);
                }
            }
        }

        $.ajax({
            url: FIND_API,
            dataType: "json",
            success: function (res) {

                // 给表单初始化数据
                layui.each(res.data, function (key, value) {
                    if ('options' === key && value) {
                        layui.each(value, function (kk, vv) {
                            try {
                                // 数组（复选框）
                                if (Array.isArray(vv)) {
                                    //init_element_attr_value('*[name="options[' + kk + '][]"]', vv)
                                    return;
                                }

                                // 对象(输入框、选择框、单选框、复选框等)
                                if ('Object' === Object.prototype.toString.call(vv).slice(8, -1)) {
                                    layui.each(vv, function (kkk, vvv) {
                                        init_element_attr_value('*[name="options[' + kk + '][' + kkk + ']"]', vvv)
                                    });
                                    return;
                                }
                            } catch (e) {
                                console.error(e)
                                return;
                            }

                            // 基础类型(输入框、选择框、单选框)
                            init_element_attr_value('*[name="options[' + kk + ']"]', vv)
                        });
                    } else {
                        init_element_attr_value('*[name="' + key + '"]', value)
                    }
                });

                // 字段 禁用 disabled
                layui.use(["form"], function () {
                    layui.$("#disabled").attr("checked", layui.$('input[name="disabled"]').val() != 0);
                    layui.form.render();
                    layui.form.on("switch(disabled)", function (data) {
                        layui.$('input[name="disabled"]').val(this.checked ? 1 : 0);
                    });
                })

                <?=$js?>

                layui.form.render();
                // ajax返回失败
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });

    //提交事件
    layui.use(["form", "popup"], function () {
        // 字段验证允许为空
        layui.form.verify({
            phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
            email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
            url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
            number: [/(^$)|^\d+$/, '只能填写数字'],
            date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
            identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
        });
        layui.form.on("submit(save)", function (data) {
            data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
            layui.$.ajax({
                url: UPDATE_API,
                type: "POST",
                dateType: "json",
                data: data.field,
                success: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    return layui.popup.success("操作成功", function () {
                        parent.refreshTable();
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                },
                complete: function () {
                },
                error: function () {
                    layui.layer.alert('请截图当前界面，求助于QQ群：' + window.IYUU_HELP.qq_qun, {
                        icon: 2,
                        title: '出错提示'
                    });
                }
            });
            return false;
        });
    });
</script>

</body>
</html>
